﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace DAL
{
    public class Access : IDb
    {
        OleDbConnection conn = new OleDbConnection();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ServerName"></param>
        /// <param name="dbName"></param>
        /// <param name="userID"></param>
        /// <param name="pwd"></param>
        public Access(string dataSource, string pwd)
        {
            conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                                    "Data Source=" + dataSource + ";Jet OLEDB:Database Password=" + pwd + ";";
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        public System.Data.DataTable ExecuteSelectCommand(IDbCommand command)
        {
            DataTable result = new DataTable();

            try
            {
                OleDbCommand oleDbCommand = (OleDbCommand)command;
                oleDbCommand.Connection = conn;
                oleDbCommand.Connection.Open();
                oleDbCommand.CommandType = CommandType.Text;
                OleDbDataReader oleDbReader = oleDbCommand.ExecuteReader(CommandBehavior.CloseConnection);
                result.Load(oleDbReader);
                oleDbReader.Close();
                return result;
            }
            catch
            {
                throw;
            }

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="selectStatement"></param>
        /// <returns></returns>
        public System.Data.DataTable ExecuteSelectCommand(string selectStatement)
        {
            DataTable result = new DataTable();
            OleDbCommand oleDbCommand = null;
            try
            {
                conn.Open();
                oleDbCommand  = new OleDbCommand(selectStatement);
                oleDbCommand.Connection = conn;
                oleDbCommand.CommandType = CommandType.Text;
                OleDbDataReader oleDbReader = oleDbCommand.ExecuteReader(CommandBehavior.CloseConnection);
                result.Load(oleDbReader);
                oleDbReader.Close();
                return result;

            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="selectStatement"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public DataTable ExecuteSelectCommand(string selectStatement, string[,] parameters)
        {
            DataTable result = new DataTable();
            try
            {
                OleDbCommand oleDbCommand = new OleDbCommand(selectStatement);

                for (int i = 0; i < parameters.Length; i++)
                {
                    oleDbCommand.Parameters.AddWithValue(parameters[i, 0], parameters[0, i]); //parameters[name, value]
                }

                conn.Open();
                OleDbDataReader oleDbReader = oleDbCommand.ExecuteReader(CommandBehavior.CloseConnection);
                result.Load(oleDbReader);
                oleDbReader.Close();
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public string ExecuteScalar(System.Data.Common.DbCommand command)
        {
            throw new NotImplementedException();
        }

        public string ExecuteScalar(string selectStatement)
        {
            throw new NotImplementedException();
        }
    }
}
